﻿/***********************************************************************
 * <copyright file="Mapper.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Wednesday, March 19, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Collections.Generic;
using System.Linq;
using DXWebApplication.SecurityServiceReference;

namespace DXWebApplication.Areas.Administrators.Models
{
    /// <summary>
    /// Mapper class
    /// </summary>
    public static class Mapper
    {
        #region Site
        /// <summary>
        /// Froms the model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static SiteDto FromModel(this SiteModel model)
        {
            if (model == null) return null;
            return new SiteDto
            {
                Id = model.Id,
                Title = model.Title,
                Description = model.Description,
                Controller = model.Controller,
                Order = model.Order,
                ParentId = model.ParentId == 0 ? null : model.ParentId,
                Status = model.Status,
                IsGroup = model.IsGroup
            };
        }

        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static SiteModel ToModel(this SiteDto dto)
        {
            if (dto == null) return null;

            return new SiteModel
            {
                Id = dto.Id,
                Title = dto.Title,
                Description = dto.Description,
                Order = dto.Order,
                ParentId = dto.ParentId,
                Controller = dto.Controller,
                IsGroup = dto.IsGroup,
                Status = dto.Status,
                PermissionInSites = dto.PermissionInSites
            };
        }

        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dtos">The dtos.</param>
        /// <returns></returns>
        public static List<SiteModel> ToModel(this List<SiteDto> dtos)
        {
            return dtos == null ? null : dtos.Select(item => item.ToModel()).ToList();
        }

        #endregion

        #region Permission
        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dtos">The dtos.</param>
        /// <returns></returns>
        public static IList<PermissionModel> ToModel(this IList<PermissionDto> dtos)
        {
            return dtos == null ? null : dtos.Select(item => item.ToModel()).ToList();
        }

        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static PermissionModel ToModel(this PermissionDto dto)
        {
            return new PermissionModel
            {
                Id = dto.Id,
                Title = dto.Title,
                Code = dto.Code,
                Description = dto.Description,
                Order = dto.Order,
                Status = dto.Status,
                ShowDefault = dto.ShowDefault,
                HasImage = dto.HasImage,
                MultiRow = dto.MultiRow
            };
        }

        /// <summary>
        /// Froms the model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static PermissionDto FromModel(this PermissionModel model)
        {
            return new PermissionDto
            {
                Id = model.Id,
                Title = model.Title,
                Code = model.Code,
                Description = model.Description,
                Order = model.Order,
                Status = model.Status,
                ShowDefault = model.ShowDefault,
                HasImage = model.HasImage,
                MultiRow = model.MultiRow
            };
        }

        /// <summary>
        /// Froms the model.
        /// </summary>
        /// <param name="models">The models.</param>
        /// <returns></returns>
        public static IList<PermissionDto> FromModel(this IList<PermissionModel> models)
        {
            return models == null ? null : models.Select(item => item.FromModel()).ToList();
        }
        #endregion

        #region PermissionInSite
        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static List<PermissionInSiteModel> ToModel(this List<PermissionInSiteDto> list)
        {
            var models = new List<PermissionInSiteModel>();
            if (list != null)
                list.ForEach(x => models.Add(x.ToModel()));
            return models;
        }

        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static PermissionInSiteModel ToModel(this PermissionInSiteDto dto)
        {
            return new PermissionInSiteModel
            {
                PermissionId = dto.PermissionId,
                SiteId = dto.SiteId
            };
        }

        /// <summary>
        /// To the business.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        public static List<PermissionInSite> ToBusiness(this List<PermissionInSiteDto> list)
        {
            var models = new List<PermissionInSite>();
            if (list != null)
                list.ForEach(x => models.Add(x.ToBusiness()));
            return models;
        }

        /// <summary>
        /// To the business.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static PermissionInSite ToBusiness(this PermissionInSiteDto dto)
        {
            return new PermissionInSite
            {
                PermissionId = dto.PermissionId,
                SiteId = dto.SiteId
            };
        }

        #endregion

        #region Role

        /// <summary>
        /// Froms the model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static RoleDto FromModel(this RoleModel model)
        {
            return new RoleDto
            {
                RoleId = model.RoleId,
                Title = model.Title,
                RoleName = model.RoleName,                
                Description = model.Description,
                IsActive = model.IsActive
            };
        }

        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dtos">The dtos.</param>
        /// <returns></returns>
        public static IList<RoleModel> ToModel(this IList<RoleDto> dtos)
        {
            return dtos == null ? null : dtos.Select(item => item.ToModel()).ToList();
        }

        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static RoleModel ToModel(this RoleDto dto)
        {
            return new RoleModel
            {
                RoleId = dto.RoleId,
                Title = dto.Title,
                RoleName = dto.RoleName,
                Description = dto.Description,
                IsActive = dto.IsActive
            };
        }

        #endregion

        #region User

        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <returns></returns>
        public static UserModel ToModel(this UserDto dto)
        {
            if (dto == null) return null;
            return new UserModel
            {
                UserId = dto.UserId,
                UserName = dto.UserName,
                Password = dto.Password,
                IsActive = dto.IsActive,
                IsApproved = dto.IsApproved,
                IsBlocked = dto.IsBlocked,
                FullName = dto.FullName,
                Email = dto.Email,
                Address = dto.Address,
                CreateDate = dto.CreateDate,
                Roles = dto.Roles != null ? dto.Roles.Select(x => x.RoleName).ToList() : null
            };
        }

        /// <summary>
        /// To the model.
        /// </summary>
        /// <param name="dtos">The dtos.</param>
        /// <returns></returns>
        public static IList<UserModel> ToModel(this IList<UserDto> dtos)
        {
            return dtos.Select(item => item.ToModel()).ToList();
        }

        /// <summary>
        /// Froms the model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static UserDto FromModel(this UserModel model)
        {
            if (model == null) return null;
            var dtos = new List<RoleDto>();
            model.Roles.ForEach(x => dtos.Add(new RoleDto { RoleName = x }));
            return new UserDto
            {
                UserId = model.UserId,
                UserName = model.UserName,
                Password = model.Password,
                IsActive = model.IsActive,
                IsApproved = model.IsApproved,
                IsBlocked = model.IsBlocked,
                FullName = model.FullName,
                Email = model.Email,
                Address = model.Address,
                Roles = dtos.ToArray()
            };
        }

        /// <summary>
        /// Froms the user information model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static UserDto FromUserInfoModel(this UserModel model)
        {
            return new UserDto
            {
                UserId = model.UserId,
                UserName = model.UserName,
                Password = model.Password,
                FullName = model.FullName,
                Email = model.Email,
                Address = model.Address
            };
        }
        #endregion
    }
}